package com.company.loganalyzer.dto;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * TraceId分析响应DTO
 * 包含问题摘要、详细分析、解决步骤和相关日志片段
 */
public class TraceAnalysisResponse {
    
    private String traceId;
    private String status; // "success", "failure", "partial_success"
    private LocalDateTime timestamp;
    
    // 核心分析结果
    private String problemSummary;      // 问题摘要
    private String detailedAnalysis;    // 详细分析
    private List<String> solutionSteps; // 具体解决步骤
    private List<LogSnippet> relatedLogs; // 相关日志片段
    
    // 链路分析信息
    private TraceChainAnalysis chainAnalysis;
    
    // 统计信息
    private TraceStatistics statistics;
    
    // 元数据
    private Map<String, Object> metadata;
    
    // 构造函数
    public TraceAnalysisResponse() {}
    
    public TraceAnalysisResponse(String traceId) {
        this.traceId = traceId;
        this.timestamp = LocalDateTime.now();
    }
    
    // Static factory methods
    public static TraceAnalysisResponse success(String traceId) {
        TraceAnalysisResponse response = new TraceAnalysisResponse(traceId);
        response.setStatus("success");
        return response;
    }
    
    public static TraceAnalysisResponse failure(String traceId, String error) {
        TraceAnalysisResponse response = new TraceAnalysisResponse(traceId);
        response.setStatus("failure");
        response.setProblemSummary("分析失败: " + error);
        return response;
    }
    
    // Getters and Setters
    public String getTraceId() {
        return traceId;
    }
    
    public void setTraceId(String traceId) {
        this.traceId = traceId;
    }
    
    public String getStatus() {
        return status;
    }
    
    public void setStatus(String status) {
        this.status = status;
    }
    
    public LocalDateTime getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(LocalDateTime timestamp) {
        this.timestamp = timestamp;
    }
    
    public String getProblemSummary() {
        return problemSummary;
    }
    
    public void setProblemSummary(String problemSummary) {
        this.problemSummary = problemSummary;
    }
    
    public String getDetailedAnalysis() {
        return detailedAnalysis;
    }
    
    public void setDetailedAnalysis(String detailedAnalysis) {
        this.detailedAnalysis = detailedAnalysis;
    }
    
    public List<String> getSolutionSteps() {
        return solutionSteps;
    }
    
    public void setSolutionSteps(List<String> solutionSteps) {
        this.solutionSteps = solutionSteps;
    }
    
    public List<LogSnippet> getRelatedLogs() {
        return relatedLogs;
    }
    
    public void setRelatedLogs(List<LogSnippet> relatedLogs) {
        this.relatedLogs = relatedLogs;
    }
    
    public TraceChainAnalysis getChainAnalysis() {
        return chainAnalysis;
    }
    
    public void setChainAnalysis(TraceChainAnalysis chainAnalysis) {
        this.chainAnalysis = chainAnalysis;
    }
    
    public TraceStatistics getStatistics() {
        return statistics;
    }
    
    public void setStatistics(TraceStatistics statistics) {
        this.statistics = statistics;
    }
    
    public Map<String, Object> getMetadata() {
        return metadata;
    }
    
    public void setMetadata(Map<String, Object> metadata) {
        this.metadata = metadata;
    }
    
    // 内部类 - 日志片段
    public static class LogSnippet {
        private String timestamp;
        private String level;
        private String serviceName;
        private String message;
        private String className;
        private String methodName;
        private String stackTrace;
        
        // 构造函数
        public LogSnippet() {}
        
        public LogSnippet(String timestamp, String level, String serviceName, String message) {
            this.timestamp = timestamp;
            this.level = level;
            this.serviceName = serviceName;
            this.message = message;
        }
        
        // Getters and Setters
        public String getTimestamp() { return timestamp; }
        public void setTimestamp(String timestamp) { this.timestamp = timestamp; }
        
        public String getLevel() { return level; }
        public void setLevel(String level) { this.level = level; }
        
        public String getServiceName() { return serviceName; }
        public void setServiceName(String serviceName) { this.serviceName = serviceName; }
        
        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
        
        public String getClassName() { return className; }
        public void setClassName(String className) { this.className = className; }
        
        public String getMethodName() { return methodName; }
        public void setMethodName(String methodName) { this.methodName = methodName; }
        
        public String getStackTrace() { return stackTrace; }
        public void setStackTrace(String stackTrace) { this.stackTrace = stackTrace; }
    }
    
    // 内部类 - 链路分析
    public static class TraceChainAnalysis {
        private List<String> serviceChain;     // 服务调用链
        private String errorService;          // 出错的服务
        private String errorOperation;        // 出错的操作
        private String rootCause;             // 根因分析
        private long totalDuration;           // 总耗时(ms)
        private Map<String, Long> serviceDurations; // 各服务耗时
        
        // 构造函数
        public TraceChainAnalysis() {}
        
        // Getters and Setters
        public List<String> getServiceChain() { return serviceChain; }
        public void setServiceChain(List<String> serviceChain) { this.serviceChain = serviceChain; }
        
        public String getErrorService() { return errorService; }
        public void setErrorService(String errorService) { this.errorService = errorService; }
        
        public String getErrorOperation() { return errorOperation; }
        public void setErrorOperation(String errorOperation) { this.errorOperation = errorOperation; }
        
        public String getRootCause() { return rootCause; }
        public void setRootCause(String rootCause) { this.rootCause = rootCause; }
        
        public long getTotalDuration() { return totalDuration; }
        public void setTotalDuration(long totalDuration) { this.totalDuration = totalDuration; }
        
        public Map<String, Long> getServiceDurations() { return serviceDurations; }
        public void setServiceDurations(Map<String, Long> serviceDurations) { this.serviceDurations = serviceDurations; }
    }
    
    // 内部类 - 统计信息
    public static class TraceStatistics {
        private int totalLogs;                 // 总日志数
        private int errorLogs;                 // 错误日志数
        private int warnLogs;                  // 警告日志数
        private Map<String, Integer> serviceLogCounts; // 各服务日志数量
        private Map<String, Integer> errorTypes; // 错误类型统计
        
        // 构造函数
        public TraceStatistics() {}
        
        // Getters and Setters
        public int getTotalLogs() { return totalLogs; }
        public void setTotalLogs(int totalLogs) { this.totalLogs = totalLogs; }
        
        public int getErrorLogs() { return errorLogs; }
        public void setErrorLogs(int errorLogs) { this.errorLogs = errorLogs; }
        
        public int getWarnLogs() { return warnLogs; }
        public void setWarnLogs(int warnLogs) { this.warnLogs = warnLogs; }
        
        public Map<String, Integer> getServiceLogCounts() { return serviceLogCounts; }
        public void setServiceLogCounts(Map<String, Integer> serviceLogCounts) { this.serviceLogCounts = serviceLogCounts; }
        
        public Map<String, Integer> getErrorTypes() { return errorTypes; }
        public void setErrorTypes(Map<String, Integer> errorTypes) { this.errorTypes = errorTypes; }
    }
}